<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :form_data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value =
            header_list[e].type == 'input_number' ? ['', ''] : ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = item.type == 'input_number' ? ['', ''] : ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div>我的提成</div>
        <div class="flex">
          <!-- <el-button type="primary" @click="show_box = true">新增</el-button> -->
          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        border
        v-loading="loading"
        :default-sort="{ prop: 'name', order: 'descending' }"
        :row-style="{
          height:
            height_size == 1 ? '58px' : height_size == 2 ? '48px' : '38px',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="项目名称"
          prop="projectName"
          key="项目名称"
          sortable
          v-if="header_list[0].showTable"
        ></el-table-column>
        <el-table-column
          label="任务名称"
          prop="assignTaskName"
          key="任务名称"
          sortable
          v-if="header_list[1].showTable"
        >
          <template #default="scope"
            >{{ scope.row.assignTaskName || scope.row.rewardDeductionTitle }}
          </template>
        </el-table-column>
        <el-table-column
          label="提成类型"
          prop="taskCode"
          key="提成类型"
          sortable
          v-if="header_list[2].showTable"
        >
          <template #default="scope">
            <div v-for="item in type_list" :key="item.id">
              <span v-if="item.dictCode == scope.row.type">{{
                item.dictValue
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="角色"
          key="角色"
          sortable
          v-if="header_list[3].showTable"
        >
          <template #default="scope">
            <div v-if="scope.row.rewardRole == 1">设计成员</div>
            <div v-if="scope.row.rewardRole == 2">设计经理</div>
            <div v-if="scope.row.rewardRole == 3">设计组长</div>
          </template>
        </el-table-column>
        <el-table-column
          label="面积"
          prop="area"
          key="面积"
          sortable
          v-if="header_list[4].showTable"
        ></el-table-column>
        <el-table-column
          label="单价"
          key="单价"
          sortable
          prop="price"
          v-if="header_list[5].showTable"
        ></el-table-column>
        <el-table-column
          width="120"
          label="总金额/总扣款"
          prop="amount"
          key="总金额/总扣款"
          sortable
          v-if="header_list[6].showTable"
        ></el-table-column>
        <el-table-column
          width="120"
          label="占比（%） "
          prop="proportion"
          key="占比（%）"
          sortable
          v-if="header_list[7].showTable"
        ></el-table-column>
        <el-table-column
          width="120"
          label="权重（%） "
          prop="assignWeight"
          key="权重（%）"
          sortable
          v-if="header_list[8].showTable"
        ></el-table-column>
        <el-table-column
          label="个人金额"
          prop="personalAmount"
          key="个人金额"
          sortable
          v-if="header_list[9].showTable"
        ></el-table-column>
        <el-table-column
          width="120"
          label="个人实际金额"
          prop="actualAmount"
          key="个人实际金额"
          sortable
          v-if="header_list[10].showTable"
        ></el-table-column>
        <el-table-column
          width="120"
          label="创建时间"
          prop="createTime"
          key="创建时间"
          sortable
          v-if="header_list[11].showTable"
          ><template #default="scope">
            <div v-if="scope.row.createTime ?? '' !== ''">
              {{ formatDate(parseInt(scope.row.createTime), 'YYYY-MM-DD') }}
            </div>
            <div v-else>-- --</div>
          </template></el-table-column
        >
        <el-table-column
          label="创建人"
          prop="createByName"
          key="创建人"
          sortable
          v-if="header_list[12].showTable"
        ></el-table-column>
        <el-table-column
          label="原因"
          prop="reason"
          key="原因"
          show-overflow-tooltip
          sortable
          v-if="header_list[13].showTable"
        ></el-table-column>
        <el-table-column
          label="核销状态"
          prop="overWorkHours"
          key="核销状态"
          sortable
          v-if="header_list[14].showTable"
        >
          <template #default="scope">
            <div v-if="scope.row.verificationStatus == false">未核销</div>
            <div v-else>已核销</div>
          </template>
        </el-table-column>
        <el-table-column
          label="核销时间"
          prop="verificationTime"
          key="核销时间"
          sortable
          v-if="header_list[15].showTable"
          ><template #default="scope">
            <div v-if="scope.row.verificationTime ?? '' !== ''">
              {{
                formatDate(parseInt(scope.row.verificationTime), 'YYYY-MM-DD')
              }}
            </div>
            <div v-else>-- --</div>
          </template></el-table-column
        >
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="list_debounce()"
        @current-change="list_debounce()"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import {
  check_role,
  formatDate,
  debounce,
  handle_search_key,
  dictData,
} from '@/utils/check_role' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted, computed } from 'vue'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useStore } from 'vuex'
const store = useStore()
const list = ref([])
const total = ref(0)
const max_height = ref()
const loading = ref(false)
const height_size = ref(1)
const header_list = ref<any>([
  {
    label: '项目名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '任务名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '提成类型',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: computed(() => {
      return type_list.value
    }),
  },
  {
    label: '角色',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      { value: '1', label: '设计成员' },
      { value: '2', label: '设计经理' },
      { value: '3', label: '设计组长' },
    ],
  },
  {
    label: '面积',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '单价',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '总金额/总扣款',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '占比（%）',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '权重（%）',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '个人金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '个人实际金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '创建时间',
    value: '',
    showTable: true,
    type: 'date',
    isShow: false,
    options: [],
  },
  {
    label: '创建人',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '原因',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '核销状态',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      { value: true, label: '已核销' },
      { value: false, label: '未核销' },
    ],
  },
  {
    label: '核销时间',
    value: '',
    showTable: true,
    type: 'date',
    isShow: false,
    options: [],
  },
])
const page_data = ref({
  current: 1,
  pageSize: 10,
  enterpriseId: store.state.enterprise_id,
  projectName: '',
  assignTaskName: '',
  type: '',
  rewardRole: '',

  area: '',
  price: '',
  amount: '',
  proportion: '',

  assignWeight: '',
  personalAmount: '',
  actualAmount: '',

  createTime: '',
  createByName: '',
  reason: '',
  verificationStatus: '',
  verificationTime: '',
})
const type_list = ref([])
onMounted(() => {
  max_height.value = window.innerHeight - 320 // 带分页的 table 高度
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('刷新成功！')
  })

  get_list()
  dictData('1710547766888894466').then((res) => {
    type_list.value = res as any
    type_list.value.forEach((i) => {
      i.label = i.dictValue
      i.value = i.dictCode
    })
  }) // 字典中 提成类型 对应的值
})
onUnmounted(() => {
  EventBus.off('refresh')
})
const get_list = () => {
  loading.value = true
  axios
    .request({
      url: serviceApi.myCommissionPage,
      method: 'post',
      data: handle_search_key(page_data.value),
    })
    .then((res) => {
      console.log(res)
      list.value = res.data.content
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((err) => {
      ElMessage.error(err)
      loading.value = false
    })
}
let list_debounce = debounce(200, get_list)
const search_key = () => {
  page_data.value = {
    current: 1,
    pageSize: 10,
    enterpriseId: store.state.enterprise_id,
    projectName: header_list.value[0].value,
    assignTaskName: header_list.value[1].value,
    type: header_list.value[2].value,
    rewardRole: header_list.value[3].value,
    area: header_list.value[4].value,
    price: header_list.value[5].value,
    amount: header_list.value[6].value,
    proportion: header_list.value[7].value,
    assignWeight: header_list.value[8].value,
    personalAmount: header_list.value[9].value,
    actualAmount: header_list.value[10].value,
    createTime: header_list.value[11].value,
    createByName: header_list.value[12].value,
    reason: header_list.value[13].value,
    verificationStatus:
      header_list.value[14].value == undefined
        ? false
        : header_list.value[14].value,
    verificationTime: header_list.value[15].value,
  }
  get_list()
}
</script>

<style lang="scss" scoped>
.top_item {
  border: 1px solid #ccc;
  cursor: pointer;
  padding: 5px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  &:hover {
    color: #3580eb;
    border-color: #3580eb;
  }
}
.line {
  background-color: #333;
  width: 1px;
  height: 16px;
  margin: 0 10px;
}
</style>
